package com.qf.admin.controller;


import com.qf.common.http.Result;
import com.qf.dto.AdminModule;
import com.qf.dto.AdminModuleRole;
import com.qf.dto.AdminRole;
import com.qf.po.LoginAdmin;
import com.qf.service.impl.AdminModuleRoleService;
import com.qf.service.impl.AdminModuleService;
import com.qf.service.impl.AdminRoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;


import java.util.List;

@Controller
@RequestMapping("permission")
public class AdminRoleController {



    //角色
    @Resource
    private AdminRoleService adminRoleService;

    //权限
    @Resource
    private AdminModuleService adminModuleService;

    //信息存在中间表
    @Resource
    private AdminModuleRoleService adminModuleRoleService;


    @RequestMapping("query")
    public String toQuery(Model model){

        List<AdminRole> list = adminRoleService.list();
        List<AdminModule> moduleList = adminModuleService.list();
        model.addAttribute("moduleList",moduleList);
        model.addAttribute("roleList",list);
        return "permission/permission";
    }
    public class RoleInfo {
        private AdminRole adminRole;//角色表
        private List<AdminModuleRole> adminModuleRoleList;//角色所对应的中间表 可以获取到角色所对应的权限

        public RoleInfo() {
        }

        public RoleInfo(AdminRole adminRole, List<AdminModuleRole> adminModuleRoleList) {
            this.adminRole = adminRole;
            this.adminModuleRoleList = adminModuleRoleList;
        }

        public AdminRole getAdminRole() {
            return adminRole;
        }

        public void setAdminRole(AdminRole adminRole) {
            this.adminRole = adminRole;
        }

        public List<AdminModuleRole> getAdminModuleRoleList() {
            return adminModuleRoleList;
        }

        public void setAdminModuleRoleList(List<AdminModuleRole> adminModuleRoleList) {
            this.adminModuleRoleList = adminModuleRoleList;
        }
    }
    @PostMapping("save")
    @ResponseBody
    public Result<?> toSave(AdminRole adminRole , @RequestParam("adminRoles") String adminRoles){
        //adminRole.setCreateTime(new Date());
        adminRoleService.saveRole(adminRole, adminRoles);
        return Result.success();
    }

    @RequestMapping("del")
    @ResponseBody
    public Result<?> toDel(@RequestParam(value = "id", required = true)Long id){
        adminRoleService.removeById(id);
        return Result.success();
    }

    @PostMapping("getById")
    @ResponseBody
    public Result<?> getById(@RequestParam(value = "id", required = true)Long id , LoginAdmin loginAdmin) {
       //* //第一个是角色表里面的Id    第二个是中间表  角色表所对应中间表的权限id就可以查出角色所对应的权限了
        AdminRole adminRole = adminRoleService.getById(id);//前端选择角色的id
     //当前登入的角色id
        List<AdminModuleRole> adminModuleRoleList = adminModuleRoleService.getByRoleId(loginAdmin.getRoleId());
        //使用内部类把两个参数同时传到前端
        return Result.success(new RoleInfo( adminRole , adminModuleRoleList));//*return Result.success();
    }
}
